package com.yaolaizaixian.merchant.home;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.base.BaseActivity;
import com.yaolaizaixian.merchant.base.BaseInfo;
import com.yaolaizaixian.merchant.base.BaseModel;
import com.yaolaizaixian.merchant.bean.CreadOrderModel;
import com.yaolaizaixian.merchant.bean.CustomerInfo;
import com.yaolaizaixian.merchant.bean.OrderLsfp;
import com.yaolaizaixian.merchant.bean.OrderLsm;
import com.yaolaizaixian.merchant.utils.AppManager;
import com.yaolaizaixian.merchant.utils.GsonUtil;
import com.yaolaizaixian.merchant.utils.LogUtil;
import com.yaolaizaixian.merchant.utils.MyNetUtils;
import com.yaolaizaixian.merchant.utils.Toasty;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/4/27.
 */
public class CancelOrderActivity extends BaseActivity {


    @BindView(R.id.ll_back)
    AutoLinearLayout llBack;
    @BindView(R.id.tv_title_text)
    TextView tvTitleText;
    @BindView(R.id.tv_cancle_name)
    TextView tvCancleName;
    @BindView(R.id.tv_cancle_totalmoney)
    TextView tvCancleTotalmoney;
    @BindView(R.id.dashLine2)
    LinearLayout dashLine2;
    @BindView(R.id.tv_cancle_youhui)
    TextView tvCancleYouhui;
    @BindView(R.id.tv_cancle_dikou)
    TextView tvCancleDikou;
    @BindView(R.id.tv_cancle_pay)
    TextView tvCanclePay;
    @BindView(R.id.dashLine3)
    LinearLayout dashLine3;
    @BindView(R.id.tv_cancle_money)
    TextView tvCancleMoney;
    @BindView(R.id.rl_cancel_no)
    AutoRelativeLayout rlCancelNo;


    private String TAG = "CancelOrderActivity";

    private Handler mhandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case 1:

                    String result = (String) msg.obj;
                    LogUtil.LogShitou(TAG, result);
                    BaseModel baseModel = GsonUtil.jsonToBean(result, BaseModel.class);
                    if (baseModel.isFlag()) {

                        CreadOrderModel creadOrderModel = GsonUtil.jsonToBean(result, CreadOrderModel.class);
                        OrderLsm orderLsm = creadOrderModel.getData().getLsm();
                        CustomerInfo customerInfo = creadOrderModel.getData().getCustomer();
                        OrderLsfp orderLsfp = creadOrderModel.getData().getLsfp();
                        //订单id
                        String sysno = orderLsm.getSysno();

                        if (!TextUtils.isEmpty(customerInfo.getCustomername())&&!TextUtils.isEmpty(customerInfo.getCustomerid())){
                            tvCancleName.setText(customerInfo.getCustomername().substring(0,1)+"先生/女士("+customerInfo.getCustomerid().substring(0,3)+"****"+customerInfo.getCustomerid().substring(7)+")");
                        }else if (!TextUtils.isEmpty(customerInfo.getCustomername())&TextUtils.isEmpty(customerInfo.getCustomerid())){
                            tvCancleName.setText(customerInfo.getCustomername().substring(0,1)+"先生/女士");
                        }else if (TextUtils.isEmpty(customerInfo.getCustomername())&!TextUtils.isEmpty(customerInfo.getCustomerid())){
                            tvCancleName.setText(customerInfo.getCustomerid().substring(0,3)+"****"+customerInfo.getCustomerid().substring(7));
                        }
                        tvCancleTotalmoney.setText("￥"+orderLsfp.getTotalAmt());
                        tvCancleYouhui.setText("-￥"+orderLsfp.getDerateAmt());
                        tvCancleDikou.setText("-￥"+orderLsfp.getCoinAmt());
                        //167：支付宝，168：微信，185：银联，184：金币，166:线下刷卡，169：现金，186:支票： ,187:线下转账

                        if (!TextUtils.isEmpty(orderLsfp.getPayTypeSysno()))
                        {
                            if ("168".equals(orderLsfp.getPayTypeSysno())){

                                tvCanclePay.setText("微信支付");
                            }
                            //之前的订单，全部归结为线下
                            else {

                                tvCanclePay.setText("线下支付");
                            }

                        }



                        tvCancleMoney.setText("￥"+orderLsfp.getPayPrice());

                    } else {

                        //如果是账号被停用，就踢出到登录页面
                        if (baseModel.getErrorCode().equals(BaseInfo.LogOutCode)) {
                            showConflictDialog(CancelOrderActivity.this, "账号被停用");

                        } else if (baseModel.getErrorCode().equals(BaseInfo.LogOutCode1)) {

                            showConflictDialog(CancelOrderActivity.this, "您的账号在其它设备登录");

                        } else {
                            Toast.makeText(CancelOrderActivity.this, baseModel.getErrorMsg(), Toast.LENGTH_SHORT);
                            return;
                        }
                    }

                    break;

            }
        }
    };
    private String sysno;

    @Override
    public void initView() {

        setContentView(R.layout.activity_cancleorder);

    }

    @Override
    public void initData() {

        tvTitleText.setText("订单详情");
        sysno = getIntent().getStringExtra("sysno");
        getData();
          //订单id

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

    }


    @OnClick({R.id.ll_back, R.id.rl_cancel_no})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.rl_cancel_no:

                //关闭
                finish();
                AppManager.getAppManager().finishAllActivity();

                break;
        }
    }


    //订单详情
    private void getData() {

        try {

            HashMap<String, String> sortedParams = new HashMap<String, String>();

            final String url = BaseInfo.URL + "lifeOrderController/lifeOrderInfo.do";
            // sysNo 值  3des加密
            //订单id
            sortedParams.put("lifeOrderSysno", sysno);
            OkHttpUtils
                    .post()
                    .url(url)
                    .params((HashMap) MyNetUtils.getInstance().getNetData(sortedParams, this))
                    .build()
                    .execute(new MyStringCallback());

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    class MyStringCallback extends StringCallback {
        @Override
        public void onError(Call call, Exception e, int id) {

            Toasty.error(CancelOrderActivity.this, "请检查网络", Toast.LENGTH_SHORT, true).show();
            Log.e(TAG, e.toString());

        }

        @Override
        public void onResponse(String response, int id) {

            Message message = new Message();
            message.what = 1;
            message.obj = response;
            mhandler.sendMessage(message);
        }
    }
}
